package com.iamback.dy.task.taskjob;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.iamback.dy.config.redis.RedisTemplateUtil;
import com.iamback.dy.entity.ContentExuser;
import com.iamback.dy.entity.ContentUnreviewed;
import com.iamback.dy.entity.DyuserBlocked;
import com.iamback.dy.mapper.ContentExuserMapper;
import com.iamback.dy.mapper.ContentUnreviewedMapper;
import com.iamback.dy.mapper.DyuserBlockedMapper;


@Component
public class TaskServicejob {
	@Autowired
	private RedisTemplateUtil redisTemplateUtil;
	@Autowired
	private ContentUnreviewedMapper contentUnreviewedMapper;
	@Autowired
	private ContentExuserMapper contentExuserMapper;
	@Autowired
	private DyuserBlockedMapper blockedMapper;
	/**
	 * 数据导入
	 */
	@Scheduled(fixedRate = 180000)
	public void pullTask(){
		 List<ContentUnreviewed> contentUnrevieweds = contentUnreviewedMapper.getTaskContentUnreviewed();
		if(!contentUnrevieweds.isEmpty()){
			List<Long> ids = new ArrayList<Long>();
			List<ContentExuser> contentExusers=new ArrayList<ContentExuser>();
			for(ContentUnreviewed entity:contentUnrevieweds){
				ContentExuser contentExuser =new ContentExuser();
				contentExuser.setDyContentId(entity.getDyContentId());
				contentExuser.setPublisherDyId(entity.getPublisherDyId());
				contentExuser.setPublisherDyName("");
				contentExuser.setContentType(entity.getContentType());
				contentExuser.setUploadDate(entity.getUploadDate());
				contentExuser.setContentText(entity.getContentText());
				contentExuser.setContentUrl(entity.getContentUrl());
				contentExuser.setIsApproved(entity.getIsApproved());
				contentExuser.setVideoDisplay(entity.getVideoDisplay());
				contentExuser.setPixelHeight(entity.getPixelHeight());
				contentExuser.setPixelWidth(entity.getPixelWidth());
				contentExuser.setDuration(entity.getDuration());
				contentExuser.setInputTime(new Date()); 
				Map<String, Object> dyuserMap =new HashMap<String, Object>();
				dyuserMap.put("dyId", entity.getPublisherDyId());
				List<DyuserBlocked> dyuserBlockeds=blockedMapper.findDyuserBlocked(dyuserMap);
				if(!dyuserBlockeds.isEmpty()){ 
					Date date =new Date();
					if(!dyuserBlockeds.get(0).getBlockedType().equals("0")){//非正常 用戶  
						if(date.before(dyuserBlockeds.get(0).getBlockedEndtime())||dyuserBlockeds.get(0).getBlockedTimeType().equals("4")){
							contentExuser.setExResult("3");
							contentExuser.setExDesc("禁言处理 系统导入自动处理为 不通过");
							//同时设置为 帖子为不通过
							entity.setIsApproved("2");
							Map<String, Object> map =new HashMap<String, Object>();
							map.put("isApproved", "2");
							map.put("dyContentId", entity.getDyContentId());
							contentUnreviewedMapper.updateContentUnrevieweByStatus(map);
						}else{
							contentExuser.setExResult("0");
						}
					}else{
						contentExuser.setExResult("0");
					} 
				}else{
					contentExuser.setExResult("0");
				}
				if(contentExuser.getExDesc()==null||contentExuser.getExDesc().equals("")){
					contentExuser.setExDesc("");
				}
				contentExusers.add(contentExuser);
				ids.add(entity.getDyContentId());
			}
			if(contentExusers.size()>0)contentExuserMapper.inserContentExuser(contentExusers);
			if(ids.size()>0)contentUnreviewedMapper.updateTaskContentUnreviewed(ids);
		}
	}
	@Scheduled(fixedRate = 600000)
	public void againDistribution(){
		Calendar nowAfter = Calendar.getInstance();
		nowAfter.add(Calendar.MINUTE, -30);
		ContentExuser contentExuser =new ContentExuser();
		contentExuser.setAllotTime(nowAfter.getTime());
		contentExuser.setExResult("0");
		contentExuserMapper.updateContentExuserBysyn(contentExuser);
		redisTemplateUtil.delvague("examin:app:user:last:");
	}
	
	
	
}
